home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / msqllib / example / simple / simple.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  2KB  |  93 lines

  1. /*
  2.         msql.library simple example
  3.         ©1998 Christophe Sollet (cfc@iname.com)
  4.  
  5.         This simple example list existing database and tables on
  6.         a mSQL database engine.
  7.  
  8.         Without argument, it will try to connect on localhost
  9. */
  10.  
  11. #include <exec/types.h>
  12. #include <proto/exec.h>
  13. #include <proto/msql.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17.  
  18. struct Library *MsqlBase;
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22.     struct MsqlConnection *co;
  23.     m_result *mre, *mre2;
  24.     m_row    mro, mro2;
  25.     char *host;
  26.  
  27.     if(argc>1)
  28.     {
  29.         if(!strcmp(argv[1], "?"))
  30.         {
  31.             printf("Usage: %s hostname\n", argv[0]);
  32.             exit(0);
  33.         }
  34.  
  35.         host = argv[1];
  36.     } else host = NULL;
  37.  
  38.     if(MsqlBase = OpenLibrary("msql.library", 3))   /* Open the library */
  39.     {
  40.         /* Before doing any msql operation, we need a valid MsqlConnection structure */
  41.         if(co = MsqlAllocConnection())
  42.         {
  43.             /* Now we'll attempt a connection on a local mSQL database engine */
  44.             if(MsqlConnect(co, host))
  45.             {
  46.                 /* Gets current DB list */
  47.                 mre = MsqlListDBs(co);
  48.                 if(mre)
  49.                 {
  50.                     printf("DB(s):\n");
  51.                     /* Now display DB list */
  52.                     while(mro = MsqlFetchRow(mre))
  53.                     {
  54.                         printf("%s\n", *mro);
  55.  
  56.                         /* For each DB, list tables */
  57.                         if(MsqlSelectDB(co, *mro) != -1)
  58.                         {
  59.                             if(mre2 = MsqlListTables(co))
  60.                             {
  61.                                 printf("\tTable(s):\n");
  62.                                 // Now display table list
  63.                                 while(mro2 = MsqlFetchRow(mre2))
  64.                                 {
  65.                                     printf("\t%s\n", *mro2);
  66.                                 }
  67.                             }
  68.                             MsqlFreeResult(mre2);
  69.                         }
  70.                         else
  71.                         {
  72.                             printf("SelectDB failed\n");
  73.                             printf("%s\n", MsqlGetErrMsg(co)); // Display err message
  74.                         }
  75.                     }
  76.                     MsqlFreeResult(mre);
  77.                 } else
  78.                 {
  79.                     printf("ListDBs failed\n");
  80.                     printf("%s\n", MsqlGetErrMsg(co)); // Display err message
  81.                 }
  82.                 MsqlClose(co);
  83.             } else
  84.             {
  85.                 printf("Connection failed\n");
  86.                 printf("%s\n", MsqlGetErrMsg(co));
  87.             }
  88.             MsqlFreeConnection(co);
  89.         } else printf("Alloc failed\n");
  90.         CloseLibrary(MsqlBase);
  91.     } else printf("Open Libs failed\n");
  92. }
  93.